package com.ruowei.modules.sys.utils;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class ZipFileUtils {
    public static void zipFiles(List<File> srcFiles, File zipFile) throws IOException {
        byte[] buf = new byte[1024];
        ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(zipFile));
        for (File srcFile : srcFiles) {
            FileInputStream inputStream = new FileInputStream(srcFile);
            outputStream.putNextEntry(new ZipEntry(srcFile.getName()));
            int len;
            while ((len = inputStream.read(buf)) > 0) {
                outputStream.write(buf, 0, len);
            }
            outputStream.closeEntry();
            inputStream.close();
        }
        outputStream.close();
    }
}
